package com.cl.security.system.service;

import com.cl.security.common.result.CommonResult;
import com.cl.security.common.util.FileUtil;
import com.cl.security.system.entity.FileInfo;
import com.cl.security.system.repository.FileInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@Service
public class FileInfoService {
    @Autowired
    FileInfoRepository fileRepository;

    public CommonResult<FileInfo> upload(MultipartFile multipartFile) {
        String name = FileUtil.upload(multipartFile);
        if (name != null) {
            FileInfo fileInfo = new FileInfo();
            fileInfo.setName(name);
            fileInfo.setSourceName(multipartFile.getOriginalFilename());
            return CommonResult.success(fileRepository.save(fileInfo));
        }
        return CommonResult.failed();
    }

    public CommonResult delete(String name) {
        if (FileUtil.delete(name)) {
            fileRepository.deleteByName(name);
            return CommonResult.success(name,"删除成功");
        }
        return CommonResult.failed("删除失败");
    }

}
